package com.example.sys2.controller;


import com.example.sys2.common.result.Result;
import com.example.sys2.common.result.ResultUtils;
import com.example.sys2.entity.Product;
import com.example.sys2.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liruirui
 * @since 2023-09-21
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService service;
    @GetMapping("/getAllList")
    public Result getAllList(){
        return (Result) ResultUtils.returnDataSuccess(service.list());
    }


    @GetMapping("/getList")
    public Object getProductList(){
        return ResultUtils.returnSuccessLayui(service.list(),service.list().size());
    }
    @GetMapping("/getById")
    public Object getById(String id){
        return ResultUtils.returnSuccess("成功",service.getById(id));
    }


    @PostMapping("/add")
    public Object addProduct(Product Product){
        if(service.save(Product)){
            return  ResultUtils.returnSuccess();
        }else{
            return  ResultUtils.returnFail();
        }
    }

    @PostMapping("/update")
    public Object updateProduct(Product Product){
        if(service.updateById(Product)){
            return  ResultUtils.returnSuccess();
        }else{
            return  ResultUtils.returnFail();
        }
    }

    @PostMapping("/delete")
    public Object deleteProduct(Product Product){
        if(service.removeById(Product.getId())){
            return  ResultUtils.returnSuccess();
        }else{
            return  ResultUtils.returnFail();
        }
    }

}
